﻿using System;
using System.Data;
using System.Data.SqlServerCe;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Common.Entities;

namespace Common.Models
{
    public class GroupRepository
    {
        public Group GetGroupById(int groupId)
        {
            using (SqlCeConnection connection = WorkDataBase.CreateConnection())
            {
                if (connection.State != ConnectionState.Open)
                    connection.Open();

                string sqlQuery = "Select IdGroup, NameGroup from Groups where IdGroup = @IdGroup";
                using (SqlCeCommand command = new SqlCeCommand(sqlQuery, connection))
                {
                    command.Parameters.Add("@IdGroup", groupId);
                    using (SqlCeDataReader dr = command.ExecuteReader())
                    {
                        if (dr.Read())
                        {
                            int idGroup = dr.GetOrdinal("IdGroup");
                            int nameGroup = dr.GetOrdinal("NameGroup");
                            Group group = new Group();
                            group.IdGroup = dr.GetInt32(idGroup);
                            group.Name = dr.GetString(nameGroup);
                            return group;
                        }
                        return null;
                    }
                }

            }
        }
    }
}
